<?php
namespace app\admin\controller;

use think\Controller;

class Common extends Base {

    /*图片上传*/
    public function uploadImage(){
        $key = key($_FILES);    //获取数组下标
        $file = request()->file( $key ); //获取文件内容
        // 移动到框架应用根目录/public/uploads/ 目录下
        $path = '../public/uploads/brand/';
        if( !is_dir( $path ) ){
            @mkdir( $path, 0777, true ); //如果目录不存在，则生成
        }
        $date = date('Ymd/');
        if( !is_dir( $path.$date ) ){
            @mkdir( $path.$date, 0777, true ); //如果目录不存在，则生成
        }
        $fileName =   $date . uniqid() . '.png';
        $image = \think\Image::open( $file );

        $res = $image-> save( $path . $fileName );
        if($res){
            echo '{"code":0,"msg":"成功上传","data":{"src":"/uploads/brand/'.$fileName.'"}}';
//     	    echo $path.$fileName;
        }else{
            echo '{"code":1,"msg":"上传失败","data":{"src":"/uploads/brand/'.$fileName.'"}}';
        }
    }

}
